
package jpoco.android;

import jpoco.client.Contact;
import android.net.Uri;

public class Util
{
	static public String getCallbackUrlScheme()
	{
		return "x-jpoco-android";
	}
	
	static public String getCallbackUrl(String providerName)
	{
		return getCallbackUrlScheme() + "://callback?provider=" + providerName;
	}

	static public boolean isCallback(Uri u)
	{
		if (u == null)
		{
			return false;
		}
		else
		{
			return u.toString().startsWith(getCallbackUrlScheme());
		}
	}
	
	static public String getToken(Uri u)
	{
		return u.getQueryParameter("oauth_token");
	}

	static public String getProviderName(Uri u)
	{
		return u.getQueryParameter("provider");
	}

	static public String getDisplayName(Contact c)
	{
		String name = null;
		
		if (c.getDisplayName() != null)
		{
			name = c.getDisplayName();
		}
		else if (c.getName() == null)
		{
			name = "unknown";
		}
		else if (c.getName().getFormatted() != null)
		{
			name = c.getName().getFormatted();
		}
		else
		{
			name = String.valueOf(c.getName().getGivenName()) + " " + String.valueOf(c.getName().getFamilyName());
		}
		
		return name;
		
	}
}
